import {shell,BrowserWindow} from 'electron'
import {join} from "path";
import icon from '../../../resources/icon.png?asset'
import { is } from '@electron-toolkit/utils'

const randomString=(len:number = 6) => {
  const possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';
  for (let i = 0; i < len; i++) {
    result += possibleChars.charAt(Math.floor(Math.random() * possibleChars.length));
  }
  return result;
}

let windows:any=[];

export default {
  // 窗口配置
  windowDefaultConfig: {
    width: 900,
    height: 670,
    show: false,
    autoHideMenuBar: true,
    frame: false,  //隐藏导航栏
    // titleBarStyle: 'hidden',
    ...(process.platform === 'linux' ? {icon} : {}),
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      sandbox: false,
      nodeIntegration: true,   //允许渲染进程使用node.js
    }
  },
  //其他额外配置
  windowExtraConfig:{
    isTop:false,//是否处于窗口最前端
    isDev:true,//是否为开发模式 显示控制台
    //todo 其他相关配置
  },
  //所有的窗口
  windows: windows,
  createWindow: function () {
    let window = new BrowserWindow(this.windowDefaultConfig)
    window.on('ready-to-show', () => {
      window.show()
    })

    window.webContents.setWindowOpenHandler((details) => {
      shell.openExternal(details.url)
      return {action: 'deny'}
    })

    if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
      window.loadURL(process.env['ELECTRON_RENDERER_URL'])
    } else {
      window.loadFile(join(__dirname, '../renderer/index.html'))
    }

    window.setAlwaysOnTop(this.windowExtraConfig.isTop);
    if(this.windowExtraConfig.isDev){
      window.webContents.openDevTools({mode: 'bottom'}); //开启调试
    }
    this.windows.push({
      id:randomString(),
      window:window,
    })
  }
}
